home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / ATLANTIS / ATLANTIS.C next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.5 KB  |  263 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED
  7.  * Permission to use, copy, modify, and distribute this software for
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission.
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  *
  28.  * US Government Users Restricted Rights
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #include <GL/glut.h>
  45. #include "atlantis.h"
  46.  
  47. fishRec sharks[NUM_SHARKS];
  48. fishRec momWhale;
  49. fishRec babyWhale;
  50. fishRec dolph;
  51.  
  52. GLboolean moving;
  53.  
  54. void
  55. InitFishs(void)
  56. {
  57.     int i;
  58.  
  59.     for (i = 0; i < NUM_SHARKS; i++) {
  60.         sharks[i].x = 70000.0 + rand() % 6000;
  61.         sharks[i].y = rand() % 6000;
  62.         sharks[i].z = rand() % 6000;
  63.         sharks[i].psi = rand() % 360 - 180.0;
  64.         sharks[i].v = 1.0;
  65.     }
  66.  
  67.     dolph.x = 30000.0;
  68.     dolph.y = 0.0;
  69.     dolph.z = 6000.0;
  70.     dolph.psi = 90.0;
  71.     dolph.theta = 0.0;
  72.     dolph.v = 3.0;
  73.  
  74.     momWhale.x = 70000.0;
  75.     momWhale.y = 0.0;
  76.     momWhale.z = 0.0;
  77.     momWhale.psi = 90.0;
  78.     momWhale.theta = 0.0;
  79.     momWhale.v = 3.0;
  80.  
  81.     babyWhale.x = 60000.0;
  82.     babyWhale.y = -2000.0;
  83.     babyWhale.z = -2000.0;
  84.     babyWhale.psi = 90.0;
  85.     babyWhale.theta = 0.0;
  86.     babyWhale.v = 3.0;
  87. }
  88.  
  89. void
  90. Init(void)
  91. {
  92.     static float ambient[] =
  93.     {0.1, 0.1, 0.1, 1.0};
  94.     static float diffuse[] =
  95.     {1.0, 1.0, 1.0, 1.0};
  96.     static float position[] =
  97.     {0.0, 1.0, 0.0, 0.0};
  98.     static float mat_shininess[] =
  99.     {90.0};
  100.     static float mat_specular[] =
  101.     {0.8, 0.8, 0.8, 1.0};
  102.     static float mat_diffuse[] =
  103.     {0.46, 0.66, 0.795, 1.0};
  104.     static float mat_ambient[] =
  105.     {0.0, 0.1, 0.2, 1.0};
  106.     static float lmodel_ambient[] =
  107.     {0.4, 0.4, 0.4, 1.0};
  108.     static float lmodel_localviewer[] =
  109.     {0.0};
  110.  
  111.     glFrontFace(GL_CW);
  112.  
  113.     glDepthFunc(GL_LEQUAL);
  114.     glEnable(GL_DEPTH_TEST);
  115.  
  116.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  117.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  118.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  119.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  120.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
  121.     glEnable(GL_LIGHTING);
  122.     glEnable(GL_LIGHT0);
  123.  
  124.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  125.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  126.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  127.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  128.  
  129.     InitFishs();
  130.  
  131.     glClearColor(0.0, 0.5, 0.9, 0.0);
  132. }
  133.  
  134. void
  135. Reshape(int width, int height)
  136. {
  137.     glViewport(0, 0, width, height);
  138.  
  139.     glMatrixMode(GL_PROJECTION);
  140.     glLoadIdentity();
  141.     gluPerspective(400.0, 2.0, 10000.0, 400000.0);
  142.     glMatrixMode(GL_MODELVIEW);
  143. }
  144.  
  145. void
  146. Animate(void)
  147. {
  148.     int i;
  149.  
  150.     for (i = 0; i < NUM_SHARKS; i++) {
  151.         SharkPilot(&sharks[i]);
  152.         SharkMiss(i);
  153.     }
  154.     WhalePilot(&dolph);
  155.     dolph.phi++;
  156.     glutPostRedisplay();
  157.     WhalePilot(&momWhale);
  158.     momWhale.phi++;
  159.     WhalePilot(&babyWhale);
  160.     babyWhale.phi++;
  161. }
  162.  
  163. /* ARGSUSED1 */
  164. void
  165. Key(unsigned char key, int x, int y)
  166. {
  167.     switch (key) {
  168.     case 27:           /* Esc will quit */
  169.         exit(1);
  170.         break;
  171.     case ' ':          /* space will advance frame */
  172.         if (!moving) {
  173.             Animate();
  174.         }
  175.     }
  176. }
  177.  
  178. void
  179. Display(void)
  180. {
  181.     int i;
  182.  
  183.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  184.  
  185.     for (i = 0; i < NUM_SHARKS; i++) {
  186.         glPushMatrix();
  187.         FishTransform(&sharks[i]);
  188.         DrawShark(&sharks[i]);
  189.         glPopMatrix();
  190.     }
  191.  
  192.     glPushMatrix();
  193.     FishTransform(&dolph);
  194.     DrawDolphin(&dolph);
  195.     glPopMatrix();
  196.  
  197.     glPushMatrix();
  198.     FishTransform(&momWhale);
  199.     DrawWhale(&momWhale);
  200.     glPopMatrix();
  201.  
  202.     glPushMatrix();
  203.     FishTransform(&babyWhale);
  204.     glScalef(0.45, 0.45, 0.3);
  205.     DrawWhale(&babyWhale);
  206.     glPopMatrix();
  207.  
  208.     glutSwapBuffers();
  209. }
  210.  
  211. void
  212. Visible(int state)
  213. {
  214.     if (state == GLUT_VISIBLE) {
  215.         if (moving)
  216.             glutIdleFunc(Animate);
  217.     } else {
  218.         if (moving)
  219.             glutIdleFunc(NULL);
  220.     }
  221. }
  222.  
  223. void
  224. menuSelect(int value)
  225. {
  226.     switch (value) {
  227.     case 1:
  228.         moving = GL_TRUE;
  229.         glutIdleFunc(Animate);
  230.         break;
  231.     case 2:
  232.         moving = GL_FALSE;;
  233.         glutIdleFunc(NULL);
  234.         break;
  235.     case 3:
  236.         exit(0);
  237.         break;
  238.     }
  239. }
  240.  
  241. int
  242. main(int argc, char **argv)
  243. {
  244.     glutInitWindowSize(500, 250);
  245.     glutInit(&argc, argv);
  246.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  247.     glutCreateWindow("GLUT Atlantis Demo");
  248.     Init();
  249.     glutDisplayFunc(Display);
  250.     glutReshapeFunc(Reshape);
  251.     glutKeyboardFunc(Key);
  252.     moving = GL_TRUE;
  253.     glutIdleFunc(Animate);
  254.     glutVisibilityFunc(Visible);
  255.     glutCreateMenu(menuSelect);
  256.     glutAddMenuEntry("Start motion", 1);
  257.     glutAddMenuEntry("Stop motion", 2);
  258.     glutAddMenuEntry("Quit", 3);
  259.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  260.     glutMainLoop();
  261.     return 0;             /* ANSI C requires main to return int. */
  262. }
  263.